package com.mdd.admin.service.course.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.yulichang.query.MPJQueryWrapper;
import com.mdd.admin.service.course.ICourseCatalogueService;
import com.mdd.admin.validate.common.PageParam;
import com.mdd.admin.validate.course.CourseCatalogueParam;
import com.mdd.admin.vo.course.CourseCatalogueListVo;
import com.mdd.admin.vo.course.CourseCatalogueDetailVo;
import com.mdd.common.core.PageResult;
import com.mdd.common.entity.course.Course;
import com.mdd.common.entity.course.CourseCatalogue;
import com.mdd.common.entity.course.CourseColumn;
import com.mdd.common.mapper.course.CourseCatalogueMapper;
import com.mdd.common.mapper.course.CourseMapper;
import com.mdd.common.utils.ArrayUtil;
import com.mdd.common.utils.TimeUtil;
import com.mdd.common.utils.UrlUtil;
import com.mdd.common.config.GlobalConfig;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import javax.annotation.Resource;
import java.util.*;

/**
 * 课程目录实现类
 *
 * @author panweiliang
 */
@Service
public class CourseCatalogueServiceImpl implements ICourseCatalogueService {

    @Resource
    CourseCatalogueMapper courseCatalogueMapper;

    @Resource
    CourseMapper courseMapper;

    /**
     * 课程目录列表
     *
     * @param pageParam 分页参数
     * @param params    搜索参数
     * @return PageResult<CourseCatalogueListVo>
     * @author panweiliang
     */
    @Override
    public PageResult<CourseCatalogueListVo> list(Integer courseId, PageParam pageParam, Map<String, String> params) {
        Integer page = pageParam.getPageNo();
        Integer limit = pageParam.getPageSize();
        QueryWrapper<CourseCatalogue> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("course_id", courseId).orderByDesc(Arrays.asList("sort"));
        courseCatalogueMapper.setSearch(queryWrapper, params, new String[]{
                "=:courseId@course_id:int",
                "like:name:str",
                "=:feeType@fee_type:int",
                "=:status@status:int"
        });
        IPage<CourseCatalogue> iPage = courseCatalogueMapper.selectPage(new Page<>(page, limit), queryWrapper);
        List<CourseCatalogueListVo> list = new LinkedList<>();
        for (CourseCatalogue item : iPage.getRecords()) {
            CourseCatalogueListVo vo = new CourseCatalogueListVo();
            BeanUtils.copyProperties(item, vo);
            vo.setCover(UrlUtil.toAbsoluteUrl(vo.getCover()));
            vo.setCreateTime(TimeUtil.timestampToDate(item.getCreateTime()));
            vo.setUpdateTime(TimeUtil.timestampToDate(item.getUpdateTime()));
            list.add(vo);
        }
        return PageResult.iPageHandle(iPage.getTotal(), iPage.getCurrent(), iPage.getSize(), list);
    }

    /**
     * 课程目录详情
     *
     * @param id 主键参数
     * @return CourseCatalogue
     * @author panweiliang
     */
    @Override
    public CourseCatalogueDetailVo detail(Integer id) {
        CourseCatalogue model = courseCatalogueMapper.selectOne(
                new QueryWrapper<CourseCatalogue>()
                        .eq("id", id)
                        .last("limit 1"));
        Assert.notNull(model, "数据不存在");
        CourseCatalogueDetailVo vo = new CourseCatalogueDetailVo();
        BeanUtils.copyProperties(model, vo);
        vo.setCover(UrlUtil.toAbsoluteUrl(vo.getCover()));

        Course course = courseMapper.selectOne(
                new QueryWrapper<Course>()
                        .eq("id", model.getCourseId())
                        .last("limit 1"));

        Assert.notNull(course, "课程数据不存在!");

        int type = course.getType() != null ? course.getType() : 0;

        if (type == 2 || type == 3) {
            vo.setContent(UrlUtil.toRelativeUrl(model.getContent()));
        }

        return vo;
    }

    /**
     * 课程目录新增
     *
     * @param courseCatalogueParam 参数
     * @author panweiliang
     */
    @Override
    public void add(CourseCatalogueParam courseCatalogueParam) {
        Course course = courseMapper.selectOne(
                new QueryWrapper<Course>()
                        .eq("id", courseCatalogueParam.getCourseId())
                        .last("limit 1"));

        int type = course.getType() != null ? course.getType() : 0;

        Assert.notNull(course, "课程数据不存在!");
        CourseCatalogue model = new CourseCatalogue();
        model.setCourseId(courseCatalogueParam.getCourseId());
        model.setName(courseCatalogueParam.getName());
        model.setFeeType(courseCatalogueParam.getFeeType());
        model.setCover(UrlUtil.toRelativeUrl(courseCatalogueParam.getCover()));
        model.setDuration(courseCatalogueParam.getDuration());
        //如果是音频 或者 视频 需要讲文件地址转换为相对路径
        if (type == 2 || type == 3) {
            model.setContent(UrlUtil.toRelativeUrl(courseCatalogueParam.getContent()));
        } else {
            model.setContent(courseCatalogueParam.getContent());
        }
        model.setStatus(courseCatalogueParam.getStatus() != null ? courseCatalogueParam.getStatus() : 1);
        model.setSort(courseCatalogueParam.getSort());
        model.setCreateTime(System.currentTimeMillis() / 1000);
        model.setUpdateTime(System.currentTimeMillis() / 1000);
        courseCatalogueMapper.insert(model);
    }

    /**
     * 课程目录编辑
     *
     * @param courseCatalogueParam 参数
     * @author panweiliang
     */
    @Override
    public void edit(CourseCatalogueParam courseCatalogueParam) {
        CourseCatalogue model = courseCatalogueMapper.selectOne(
                new QueryWrapper<CourseCatalogue>()
                        .eq("id", courseCatalogueParam.getId())
                        .last("limit 1"));
        Assert.notNull(model, "数据不存在!");

        Integer courseId = model.getCourseId();
        Course course = courseMapper.selectOne(
                new QueryWrapper<Course>()
                        .eq("id", courseId)
                        .last("limit 1"));

        int type = course.getType() != null ? course.getType() : 0;


        model.setId(courseCatalogueParam.getId());
        model.setName(courseCatalogueParam.getName());
        model.setFeeType(courseCatalogueParam.getFeeType());
        model.setCover(UrlUtil.toRelativeUrl(courseCatalogueParam.getCover()));

        if (type == 2 || type == 3) {
            model.setContent(UrlUtil.toRelativeUrl(courseCatalogueParam.getContent()));
        } else {
            model.setContent(courseCatalogueParam.getContent());
        }

        model.setSort(courseCatalogueParam.getSort());
        model.setUpdateTime(System.currentTimeMillis() / 1000);
        courseCatalogueMapper.updateById(model);
    }

    /**
     * 课程目录删除
     *
     * @param id 主键ID
     * @author panweiliang
     */
    @Override
    public void del(Integer id) {
        CourseCatalogue model = courseCatalogueMapper.selectOne(
                new QueryWrapper<CourseCatalogue>()
                        .eq("id", id)
                        .last("limit 1"));
        Assert.notNull(model, "数据不存在!");
        courseCatalogueMapper.delete(new QueryWrapper<CourseCatalogue>().eq("id", id));
    }

    @Override
    public CourseCatalogue changeStatus(Integer id) {
        CourseCatalogue courseCatalogue = courseCatalogueMapper.selectOne(
                new QueryWrapper<CourseCatalogue>()
                        .eq("id", id)
                        .last("limit 1"));
        Assert.notNull(courseCatalogue, "课程不存在!");
        courseCatalogue.setStatus(courseCatalogue.getStatus() == 1 ? 0 : 1);
        courseCatalogue.setUpdateTime(TimeUtil.timestamp());
        courseCatalogueMapper.updateById(courseCatalogue);
        return courseCatalogue;
    }

    @Override
    public CourseCatalogue changeFeeType(Integer id) {
        CourseCatalogue courseCatalogue = courseCatalogueMapper.selectOne(
                new QueryWrapper<CourseCatalogue>()
                        .eq("id", id)
                        .last("limit 1"));
        Assert.notNull(courseCatalogue, "课程目录不存在!");
        courseCatalogue.setFeeType(courseCatalogue.getFeeType() == 1 ? 0 : 1);
        courseCatalogue.setUpdateTime(TimeUtil.timestamp());
        courseCatalogueMapper.updateById(courseCatalogue);
        return courseCatalogue;
    }
}
